package com.zking.controller.jch;

import com.zking.entity.Brand;
import com.zking.entity.User;
import com.zking.service.jch.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/Manager")
public class ManagerController {

    @Autowired
    private BrandService brandService;


    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(User user) {
//        try {
//            System.out.println(user.getUsername());
//            System.out.println(user.getPassword());
//            //当前用户的所有信息
//            Subject subject = SecurityUtils.getSubject();
//            boolean authenticated = subject.isAuthenticated();
//            System.out.println("当前的这个用户是否验证：" + authenticated);
//            if (!authenticated) {
//                //用户登陆的用户名和密码，是否正确
//                UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
//                subject.login(token);
////                subject.logout();
////                token.setRememberMe(true);
//                User principal = (User) subject.getPrincipal();
//                System.out.println(principal);
//            }
//            User principal = (User) subject.getPrincipal();
//            System.out.println(principal);
//        } catch (UnknownAccountException e) {
//            System.out.println("账户不存在");
//            return "jch/login";
//        } catch (AuthenticationException e) {
//            System.out.println("密码错误");
//            e.printStackTrace();
//            return "jch/login";
//        }

        return "redirect:toIndex";


    }

    @RequestMapping("/data")
    @ResponseBody
    public Brand getData(int id) {
        Brand brand = brandService.queryBrand(id);
        return brand;
    }

    @RequestMapping(value = "/updateBrand", method = RequestMethod.GET,name = "更新品牌" )
    public String updateBrand(HttpServletRequest request) {
        String brandName2 = request.getParameter("brandName2");
        String firstChar2 = request.getParameter("firstChar2");
        int upid = Integer.parseInt(request.getParameter("upid"));

        boolean b = brandService.updateBrand(upid, brandName2, firstChar2);
        System.out.println(b);
        return "redirect:selectBrandAll";

    }


    @RequestMapping(value = "/selectBrandAll", method = RequestMethod.GET,name = "品牌查询" )
    public String selectBrandAll(HttpServletRequest request) {
        List<Brand> brands= brandService.queryBrands();
        request.setAttribute("brands",brands);
        return "jch/brand";
    }


    @RequestMapping(value = "/addBrand", method = RequestMethod.GET,name = "添加品牌" )
    public String addBrand(HttpServletRequest request) {
        String brandName = request.getParameter("brandName");
        String firstChar = request.getParameter("firstChar");
        boolean b = brandService.addBrand(brandName, firstChar);
        return "redirect:selectBrandAll";
    }

    @RequestMapping(value = "delBrand",method = RequestMethod.GET, name = "删除商品")
    public String delBrand(HttpServletRequest request){
        String ids = request.getParameter("ids");
        if (ids == null){
            return "redirect:selectBrandAll";
        }

        String[] split = ids.split(",");
        int length = split.length;
        int[] id = new int[length];
        System.out.println(split);
        for (int i = 0; i < length ; i++) {
            id[i] = Integer.parseInt(split[i]);
            boolean b = brandService.delBrand(id[i]);
            System.out.println(b);
        }

        return "redirect:selectBrandAll";
    }


//登录
    @RequestMapping("/toLogin")
    public String  toLogin(){
        return "jch/login";
    }
//运营商首页
    @RequestMapping("/toIndex")
    public String  toIndex(){
        return "jch/index";
    }
//首页
    @RequestMapping("/toHome")
    public String toHome(){
        return "jch/home";
    }
//商家审核
    @RequestMapping("/toExamine")
    public String toExamine(){
        return "jch/seller_1";
    }
//商家管理
    @RequestMapping("/toManager")
    public String toManager(){
        return "jch/seller";
    }
//品牌管理
    @RequestMapping("/toBrand")
    public String toBrand(){
        return "jch/brand";
    }
//规格管理
    @RequestMapping("/toSpecification")
    public String toSpecification(){
        return "jch/specification";
    }
//模板管理
    @RequestMapping("/toTemplate")
    public String toTemplate(){
        return "jch/type_template";
    }
//分类管理
    @RequestMapping("/toSort")
    public String toSort(){
        return "jch/item_cat";
    }
//商品审核
    @RequestMapping("/toGoods")
    public String toGoods(){
        return "jch/goods";
    }
}
